﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wangzeran.CodeMaker.Core.DB;
using Wangzeran.CodeMaker.Core.Template;
using Wangzeran.CodeMaker.Core.Tag;
using Wangzeran.CodeMaker.Core.Files;

namespace Wangzeran.CodeMaker.GUI.Forms
{
    /// <summary>
    /// 插件管理界面
    /// </summary>
    public partial class PlugInsForm : Form
    {
        public PlugInsForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示插件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnPlugIns_Click(object sender, EventArgs e)
        {
            if (!(sender is ToolStripButton))
            {
                return;
            }
            ToolStripButton tsbtn = sender as ToolStripButton;
            string type = tsbtn.Tag.ToString();
            switch (type)
            { 
                case "database":
                    {
                        InitDatabasePlugIns();
                        break;
                    }
                case "template":
                    {
                        InitTemplatePlugIns();
                        break;
                    }
                case "tag":
                    {
                        InitTagPlugIns();
                        break;
                    }
                case "outputfile":
                    {
                        InitOutputFilePlugIns();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        /// <summary>
        /// 初始化数据库插件
        /// </summary>
        private void InitDatabasePlugIns()
        {
            InitListViewColumns(new string[] { "数据库类型", "插件类名" }, 
                new int[] { 100, 320 });

            foreach (IDBPlugInSupport databaseType in DBPlugIns.Dbs.Values)
            {
                AddListViewItem(new string[] { databaseType.DatabaseType, 
                    databaseType.ToString() });
            }
        }

        /// <summary>
        /// 初始化数据库插件
        /// </summary>
        private void InitTemplatePlugIns()
        {
            InitListViewColumns(new string[] { "模板类型", "插件类名" }, 
                new int[] { 100, 320 });

            foreach (ITemplate templateType in TemplatePlugIns.Templates.Values)
            {
                AddListViewItem(new string[] { templateType.TemplateName, 
                    templateType.ToString() });
            }
        }

        /// <summary>
        /// 初始化数据库插件
        /// </summary>
        private void InitTagPlugIns()
        {
            InitListViewColumns(new string[] { "标签名称", "插件类名" }, new int[] { 100, 320 });

            foreach (string tagType in TagPlugIns.Tags.Keys)
            {
                AddListViewItem(new string[] { tagType, TagPlugIns.Tags[tagType] });
            }
        }

        /// <summary>
        /// 初始化数据库插件
        /// </summary>
        private void InitOutputFilePlugIns()
        {
            InitListViewColumns(new string[] { "输出文件名称", "扩展名", "插件类名" }, 
                new int[] { 100, 60, 320 });

            foreach (IFile fileType in FilePlugIns.Files.Values)
            {
                AddListViewItem(new string[] { fileType.FileDescription, 
                    fileType.FileExt, fileType.ToString() });
            }
        }


        /// <summary>
        /// 初始化列名
        /// </summary>
        /// <param name="columns"></param>
        private void InitListViewColumns(string[] columns, int[] widths)
        {
            lvPlugIns.Columns.Clear();
            for (int i = 0; i < columns.Length; i++)
			{
                lvPlugIns.Columns.Add(columns[i], widths[i]);
			}

            lvPlugIns.Items.Clear();
            tsslblCount.Text = "0";
            
        }

        /// <summary>
        /// 添加列的值
        /// </summary>
        /// <param name="columns">列值</param>
        private void AddListViewItem(string[] columns)
        {
            if (columns.Length == 0)
            {
                return;
            }
            ListViewItem item = new ListViewItem(columns[0]);
            for (int i = 1; i < columns.Length; i++)
            {
                item.SubItems.Add(columns[i]);
            }
            lvPlugIns.Items.Add(item);
            tsslblCount.Text = lvPlugIns.Items.Count.ToString();
        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
